package org.lq.er.view.admin;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;

import org.lq.er.entity.Admin;
import org.lq.er.service.AdminService;
import org.lq.er.service.impl.AdminServiceImpl;
import org.lq.er.util.AutoId;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 *	 管理员添加
 * @author 六组刘明昕
 * @date 2020-8-29 0:18:24
 */
public class V_admin_add extends JFrame {

	private JPanel contentPane;
	private JTextField txt_AdminName;
	private JTextField txt_AdminPwd;
	private JTextField txt_AdminPhone;
	private JTextField txt_AdminCard;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_admin_add frame = new V_admin_add();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_admin_add() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 435);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u7BA1\u7406\u5458\u6DFB\u52A0", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lbl_AdminName = new JLabel("姓名:");
		lbl_AdminName.setBounds(47, 51, 81, 15);
		panel.add(lbl_AdminName);
		
		txt_AdminName = new JTextField();
		txt_AdminName.setBounds(118, 48, 66, 21);
		panel.add(txt_AdminName);
		txt_AdminName.setColumns(10);
		
		JLabel lbl_AdminPwd = new JLabel("密码:");
		lbl_AdminPwd.setBounds(47, 85, 54, 15);
		panel.add(lbl_AdminPwd);
		
		txt_AdminPwd = new JTextField();
		txt_AdminPwd.setEditable(false);
		txt_AdminPwd.setText("");
		txt_AdminPwd.setBounds(118, 82, 101, 21);
		panel.add(txt_AdminPwd);
		txt_AdminPwd.setColumns(10);
		
		JLabel lbl_AdminPhone = new JLabel("手机号:");
		lbl_AdminPhone.setBounds(47, 126, 54, 15);
		panel.add(lbl_AdminPhone);
		
		txt_AdminPhone = new JTextField();
		txt_AdminPhone.setBounds(118, 123, 101, 21);
		panel.add(txt_AdminPhone);
		txt_AdminPhone.setColumns(10);
		
		JLabel lbl_AdminCard = new JLabel("身份证号:");
		lbl_AdminCard.setBounds(47, 167, 54, 15);
		panel.add(lbl_AdminCard);
		
		txt_AdminCard = new JTextField();
		txt_AdminCard.setBounds(118, 164, 101, 21);
		panel.add(txt_AdminCard);
		txt_AdminCard.setColumns(10);
		
		JButton btn_Add = new JButton("添加");
		btn_Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//如果未输入信息则进行提示,不向数据库添加
				if (txt_AdminName.getText() == null || txt_AdminPhone == null || txt_AdminCard == null) {
					JOptionPane.showMessageDialog(V_admin_add.this, "请输入基本信息");
				} else {
					Admin admin = new Admin();
					//admin.setId(AutoId.getIdNo());
					admin.setName(txt_AdminName.getText());
					admin.setPhone_no(txt_AdminPhone.getText());
					//默认密码为手机号加123
					admin.setPassword(txt_AdminPhone.getText() + "123");
					admin.setCardId(txt_AdminCard.getText());
					AdminService as = new AdminServiceImpl();
					if (as.save(admin)) {
						JOptionPane.showMessageDialog(V_admin_add.this, "添加新管理员成功", "添加", 1);
						reset();
					} 
				}
			}
		});
		btn_Add.setBounds(47, 254, 93, 23);
		panel.add(btn_Add);
		
		JButton btn_Cancel = new JButton("重置");
		btn_Cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		btn_Cancel.setBounds(261, 254, 93, 23);
		panel.add(btn_Cancel);
	}
	public void reset() {
		txt_AdminName.setText("");
		txt_AdminPhone.setText("");
		txt_AdminPwd.setText("");
	}
}
